<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<link href="${pageContext.request.contextPath}/css/style.css" rel="stylesheet" type="text/css"/>
<link href="${pageContext.request.contextPath}/css/table.css" rel="stylesheet" type="text/css"/>
<link href="${pageContext.request.contextPath}/dwz/themes/default/style.css" rel="stylesheet" type="text/css"
      media="screen"/>
<link href="${pageContext.request.contextPath}/dwz/themes/css/core.css" rel="stylesheet" type="text/css"
      media="screen"/>
<script src="${pageContext.request.contextPath}/jquery/jquery-2.1.4.min.js"></script>
<table border="0" cellpadding="0" cellspacing="0" class="table_border">
    <tr>
        <td>
            <table width="100%" border="0" cellpadding="0" cellspacing="0"
                   class="table_padding">
                <tr>
                    <td>
                        <table align="left">
                            <tr>
                                <td width="75px" id="detailBtn">
                                    <a class="button" onclick="getDetail()" href="javascript:void(0);"><span>查看明细</span>
                                    </a>
                                </td>
                                <td width="50px" id="addBtn"><a class="button"
                                                    href="common?method=resource&resource=/WEB-INF/qx/role/add.jsp"><span>新增</span></a>
                                </td>
                                <td width="50px" id="delBtn"><a class="button"
                                                    href="javascript:void(0);"
                                                    onclick="deleteRole()"><span>删除</span></a></td>
                                <td width="50px" id="updateBtn"><a class="button" href="javascript:void(0);" onclick="update()"><span>修改</span></a>
                                </td>
                                <td width="80px" id="assignBtn"><a class="button" href="javascript:void(0);"
                                                    onclick="assign()"><span>分配许可</span></a>
                                </td>
                            </tr>
                        </table>
                    </td>
                    <td width="25" height="26" align="right"></td>
                </tr>
            </table>
        </td>
    </tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td align="center">
            <table border="0" cellpadding="0" cellspacing="0" id="box_num_table2"
                   class="box_num_table">
                <thead>
                <tr bgcolor="white">
                    <td><input type="checkbox" onclick="checkAll(this.checked)" id="firstCheckBox"/></td>
                    <td>序号</td>
                    <td>代码</td>
                    <td>名称</td>
                    <td>描述</td>
                </tr>
                </thead>
                <tbody>
                <c:forEach items="${rolePage.list}" var="role" varStatus="status">
                    <tr bgcolor="white">
                        <td>
                            <input type="checkbox" class="checkbox-role"/>
                            <input type="text" hidden value="${role.id}">
                        </td>
                        <td>${status.index+1}</td>
                        <td>${role.code}</td>
                        <td>${role.name}</td>
                        <td>${role.remark}</td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </td>
    </tr>
    <tr>
        <td>
            <table border="0" cellpadding="0" cellspacing="0"
                   class="table_border">
                <tr>
                    <td>
                        <table width="100%" border="0" cellpadding="0" cellspacing="0"
                               class="table_padding">
                            <tr>
                                <td width="8%" class="font_left">数据:${rolePage.total}条</td>
                                <td width="12" class="font_left">第</td>
                                <td width="375" class="font_left">
                                    <input name="textfield2" id="pageNo" type="text" size="2" maxlength="4"
                                           value="${rolePage.pageNum}"/> /${rolePage.pages}页
                                    <input type="button" value="跳转" onclick="jumpPage()">
                                    <c:forEach items="${rolePage.navigatepageNums}" var="ite">
                                        <a href="role?method=getRolePage&pageNo=${ite}&pageSize=10">${ite}&nbsp;&nbsp;&nbsp;</a>
                                    </c:forEach>
                                </td>
                                <td width="478" class="font_right">
                                    <a href="javascript:void (0)" onclick="firstPage()">首页</a>
                                    <a href="javascript:void (0)" onclick="beforePage()">上一页</a>
                                    <a href="javascript:void (0)" onclick="nextPage()">下一页</a>
                                    <a href="javascript:void (0)" onclick="lastPage()">末页</a>
                                    <a href="role?method=getRolePage&pageNo=${rolePage.pageNum}&pageSize=10">刷新</a>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
<script>
    $(function () {
        let permission = '${permissionAll}'
        if (permission.indexOf("role?method=getRoleDetail")<0){
            $("#detailBtn").hide()
        }
        if (permission.indexOf("common?method=resource&resource=/WEB-INF/qx/role/add.jsp")<0){
            $("#addBtn").hide()
        }
        if (permission.indexOf("role?method=deleteRole")<0){
            $("#delBtn").hide()
        }
        if (permission.indexOf("role?method=getUpdateByRole")<0){
            $("#updateBtn").hide()
        }
        if (permission.indexOf("role?method=getAssign")<0){
            $("#assignBtn").hide()
        }


    })
</script>
<script>

    /**
     * 上一页
     */
    function beforePage() {
        var isFirstPage = ${rolePage.hasPreviousPage};
        if (!isFirstPage) {
            alert("已经是首页了");
            return;
        }
        location.href = "${pageContext.request.contextPath}/role?method=getRolePage&pageNo=${rolePage.prePage}&pageSize=10";
    }

    /**
     * 下一页
     */
    function nextPage() {
        var hasNextPage = ${rolePage.hasNextPage};
        if (!hasNextPage) {
            alert("已经到底了");
            return;
        }
        location.href = "${pageContext.request.contextPath}/role?method=getRolePage&pageNo=${rolePage.nextPage}&pageSize=10";
    }

    /**
     * 首页
     */
    function firstPage() {
        var isFirstPage = ${rolePage.isFirstPage};
        if (isFirstPage) {
            alert("这里已经是首页");
            return;
        }
        location.href = "${pageContext.request.contextPath}/role?method=getRolePage&pageNo=${rolePage.navigateFirstPage}&pageSize=10";
    }


    /**
     * 末页
     */
    function lastPage() {
        var isLastPage = ${rolePage.isLastPage};
        if (isLastPage) {
            alert("已经到底了");
            return;
        }
        location.href = "${pageContext.request.contextPath}/role?method=getRolePage&pageNo=${rolePage.navigateLastPage}&pageSize=10";
    }

    /**
     * 跳转指定页
     */
    function jumpPage() {
        let pageNo = $("#pageNo").val();
        location.href = "${pageContext.request.contextPath}/role?method=getRolePage&pageNo=" + pageNo + "&pageSize=10";
    }

    /**
     * 跳转更新视图
     */
    function update() {
        let checked = $(".checkbox-role:checked ~ input")[0];
        if (checked == null) {
            alert("没有选中角色,请选择一个角色")
            return;
        }
        location.href = "role?method=getUpdateByRole&id=" + checked.value;
    }

    /**
     * 跳转授权角色视图
     */
    function assign() {
        let checked = $(".checkbox-role:checked ~ input")[0];
        if (checked == null) {
            alert("没有选中角色,请选择一个角色")
            return
        }
        location.href = "role?method=getAssign&id=" + checked.value;
    }

    /**
     * 跳转查询角色视图
     */
    function getDetail() {
        let checked = $(".checkbox-role:checked ~ input")[0];
        if (checked == null) {
            alert("没有选中角色,请选择一个角色")
            return
        }
        location.href = "role?method=getRoleDetail&id=" + checked.value;
    }

    /**
     * 全选 复选框
     * @param status
     */
    function checkAll(status) {
        let checkboxList = $(":checkbox");
        for (let i = 0; i < checkboxList.length; i++) {
            checkboxList[i].checked = status;
        }
    }


    /**
     * 多选框 删除 角色
     */
    function deleteRole() {
        let checked = $(".checkbox-role:checked ~ input");
        let array = new Array();
        for (let i = 0; i < checked.length; i++) {
            array.push(checked[i].value)
        }
        $.post("role?method=deleteRole", {"array[]": array}, function (result) {
            if (result.code == "200") {
                location.href = "${pageContext.request.contextPath}/role?method=getRolePage";
            } else {
                alert(result.message);
            }
        }, "json")
    }


</script>
